package com.sinacom.ibuilding.service;




import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;


import com.ibatis.dao.client.DaoManager;
import com.sinacom.ibuilding.beans.Member;
import com.sinacom.ibuilding.beans.Schedule;
import com.sinacom.ibuilding.dao.DaoConfig;
import com.sinacom.ibuilding.dao.iface.ScheduleDao;
import com.sinacom.ibuilding.util.Constants;
import com.sinacom.ibuilding.util.PageModel;

public class ScheduleService {
	
	private ScheduleDao scheduleDao;
	private IdCreateService idService = new IdCreateService();
	public ScheduleService(){
		DaoManager	daoManager = DaoConfig.getDaoManager();
		scheduleDao = (ScheduleDao) daoManager.getDao(ScheduleDao.class);
	}
	
	public List getScheduleList(Date day){
	    Map map = new HashMap();
	    map.put("startTime", day);
	    
	    return scheduleDao.getScheduleList(map);
	}
	
	public Schedule getSechuleById(String id){
	    if(StringUtils.isEmpty(id))return null;
	    Map map = new HashMap();
        map.put("id", id);
        List list = scheduleDao.getScheduleList(map);
        if(list != null && list.size() > 0){
            return (Schedule) list.get(0);
        }
        
        return null;
	}
	public PageModel getScheduleDay(Date day,int page){
        int pageSize = 12;
        Map map = new HashMap(); 
        map.put("startTime", day);
        int startId = (page-1) * pageSize + 1; 
        map.put("startId", new Integer(startId));
        map.put("endId", new Integer(startId+pageSize));
        List list = scheduleDao.getScheduleList(map);
        PageModel pageModel = new PageModel();
        pageModel.setList(list);
        pageModel.setCurrentPage(page);
        pageModel.setPageSize(pageSize);
        
        int count = scheduleDao.getScheduleListCount(map);
        pageModel.setTotalRecord(count);
        pageModel.setTotalPage((int)Math.ceil((double)count/pageSize));
        return pageModel;
    }
	
	public List hasSchedules(Date date){
	    Map map = new HashMap();
	    map.put("startTime", date);
	    return scheduleDao.getScheduleDay(map);
	}
	
	public boolean saveSchedule(Member member,Schedule schedule){
        try{
            if(schedule != null){
                
              
                String id = null; 
                Date now = new Date();
                if(StringUtils.isEmpty(schedule.getId())){ 
                    id = String.valueOf(idService.generateId(Constants.TABLE_SCHEDULE));
                    schedule.setId(String.valueOf(id));
                    schedule.setCreator(member.getId());
                    schedule.setUpdater(member.getId());
                    
                    schedule.setModifyDate(now);
                    schedule.setCreateDate(now);
                    scheduleDao.insertSchedule(schedule);
                }else{
                    schedule.setUpdater(member.getId());
                    schedule.setModifyDate(now);
                    scheduleDao.updateSchedule(schedule); 
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
        
        return true;
    }
	
	public boolean deleteSchedule(String id){
	    try{
	        Map map = new HashMap();
	        map.put("id", id);
            scheduleDao.deleteSchedule(map);
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
	    return true;
	}
	
	public static void main(String[] args){
        ScheduleService service = new ScheduleService();
        TransActionService ts = new TransActionService();
        try {
            ts.startTransaction();
            List list = service.hasSchedules(new Date());
            System.out.println(list);
            ts.commitTransAction();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                ts.endTransAction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        
    }
}
